{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Scanners"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<IB connected to 127.0.0.1:7497 clientId=17>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from ib_insync import *\n",
    "util.startLoop()  \n",
    "\n",
    "ib = IB()\n",
    "ib.connect('127.0.0.1', 7497, clientId=17)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Basic Scanner\n",
    "\n",
    "To create a scanner, create a `ScannerSubscription` to submit to the `reqScannerData` method. For any scanner to work, at least these three fields must be filled: `instrument` (the what), `locationCode` (the where), and `scanCode` (the ranking).\n",
    "\n",
    "For example, to find the top ranked US stock percentage gainers:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "50 results, first one:\n",
      "ScanData(rank=0, contractDetails=ContractDetails(contract=Contract(secType='STK', conId=409806996, symbol='CTIB', exchange='SMART', currency='USD', localSymbol='CTIB', tradingClass='SCM'), marketName='SCM', minTick=0.0, orderTypes='', validExchanges='', priceMagnifier=0, underConId=0, longName='', contractMonth='', industry='', category='', subcategory='', timeZoneId='', tradingHours='', liquidHours='', evRule='', evMultiplier=0, mdSizeMultiplier=0, aggGroup=0, underSymbol='', underSecType='', marketRuleIds='', secIdList=[], realExpirationDate='', lastTradeTime='', stockType='', cusip='', ratings='', descAppend='', bondType='', couponType='', callable=False, putable=False, coupon=0, convertible=False, maturity='', issueDate='', nextOptionDate='', nextOptionType='', nextOptionPartial=False, notes=''), distance='', benchmark='', projection='', legsStr='')\n"
     ]
    }
   ],
   "source": [
    "sub = ScannerSubscription(\n",
    "    instrument='STK', \n",
    "    locationCode='STK.US.MAJOR', \n",
    "    scanCode='TOP_PERC_GAIN')\n",
    "\n",
    "scanData = ib.reqScannerData(sub)\n",
    "\n",
    "print(f'{len(scanData)} results, first one:')\n",
    "print(scanData[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*The displayed error 162 can be ignored*\n",
    "\n",
    "The scanner returns a list of contract details, without current market data (this can be obtained via seperate market data requests).\n",
    "\n",
    "## Filtering scanner results, the old way\n",
    "\n",
    "The `ScannerSubscription` object has addional parameters that can be set to filter the results, such as `abovePrice`, `aboveVolume`, `marketCapBelow` or `spRatingAbove`. \n",
    "\n",
    "For example, to reuse the previous `sub` and query only for stocks with a price above 200 dollar:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Error 162, reqId 3: Historical Market Data Service error message:API scanner subscription cancelled: 3\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['FVRR', 'BA', 'AMGN', 'VRTX', 'LMT']\n"
     ]
    }
   ],
   "source": [
    "sub.abovePrice = 200\n",
    "scanData = ib.reqScannerData(sub)\n",
    "\n",
    "symbols = [sd.contractDetails.contract.symbol for sd in scanData]\n",
    "print(symbols)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Filtering, the new way\n",
    "\n",
    "In the new way there is a truly vast number of parameters available to use for filtering.\n",
    "These new scanner parameters map directly to the options available through the TWS \"Advanced Market Scanner.\" The parameters\n",
    "are dynamically available from a huge XML document that is returned by ``reqScannerParameters``:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Error 162, reqId 4: Historical Market Data Service error message:API scanner subscription cancelled: 4\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2000323 bytes\n"
     ]
    }
   ],
   "source": [
    "xml = ib.reqScannerParameters()\n",
    "\n",
    "print(len(xml), 'bytes')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To view the XML in a web browser:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "path = 'scanner_parameters.xml'\n",
    "with open(path, 'w') as f:\n",
    "    f.write(xml)\n",
    "\n",
    "import webbrowser\n",
    "webbrowser.open(path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1352 tags:\n",
      "['priceAbove', 'priceBelow', 'usdPriceAbove', 'usdPriceBelow', 'volumeAbove', 'usdVolumeAbove', 'usdVolumeBelow', 'avgVolumeAbove', 'avgVolumeBelow', 'avgUsdVolumeAbove', 'avgUsdVolumeBelow', 'ihNumSharesInsiderAbove', 'ihNumSharesInsiderBelow', 'ihInsiderOfFloatPercAbove', 'ihInsiderOfFloatPercBelow', 'iiNumSharesInstitutionalAbove', 'iiNumSharesInstitutionalBelow', 'iiInstitutionalOfFloatPercAbove', 'iiInstitutionalOfFloatPercBelow', 'marketCapAbove1e6', 'marketCapBelow1e6', 'moodyRatingAbove', 'moodyRatingBelow', 'spRatingAbove', 'spRatingBelow', 'ratingsRelation', 'bondCreditRating', 'maturityDateAbove', 'maturityDateBelow', 'currencyLike', 'excludeConvertible', 'couponRateAbove', 'couponRateBelow', 'optVolumeAbove', 'optVolumeBelow', 'avgOptVolumeAbove', 'optVolumePCRatioAbove', 'optVolumePCRatioBelow', 'impVolatAbove', 'impVolatBelow', 'impVolatOverHistAbove', 'impVolatOverHistBelow', 'imbalanceAbove', 'imbalanceBelow', 'displayImbalanceAdvRatioAbove', 'displayImbalanceAdvRatioBelow', 'regulatoryImbalanceAbove', 'regulatoryImbalanceBelow', 'displayRegulatoryImbAdvRatioAbove', 'displayRegulatoryImbAdvRatioBelow', 'avgRatingAbove', 'avgRatingBelow', 'numRatingsAbove', 'numRatingsBelow', 'avgPriceTargetAbove', 'avgPriceTargetBelow', 'numPriceTargetsAbove', 'numPriceTargetsBelow', 'avgAnalystTarget2PriceRatioAbove', 'avgAnalystTarget2PriceRatioBelow', 'stkTypes', 'hasOptionsIs', 'leadFutOnly', 'dividendFrdAbove', 'dividendFrdBelow', 'dividendYieldFrdAbove', 'dividendYieldFrdBelow', 'dividendNextDateAbove', 'dividendNextDateBelow', 'dividendNextAmountAbove', 'dividendNextAmountBelow', 'histDividendFrdAbove', 'histDividendFrdBelow', 'histDividendFrdYieldAbove', 'histDividendFrdYieldBelow', 'minGrowthRate', 'maxGrowthRate', 'minPeRatio', 'maxPeRatio', 'minQuickRatio', 'maxQuickRatio', 'minRetnOnEq', 'maxRetnOnEq', 'minPrice2Bk', 'maxPrice2Bk', 'minPrice2TanBk', 'maxPrice2TanBk', 'firstTradeDateAbove', 'firstTradeDateBelow', 'changePercAbove', 'changePercBelow', 'afterHoursChangePercAbove', 'afterHoursChangePercBelow', 'changeOpenPercAbove', 'changeOpenPercBelow', 'openGapPercAbove', 'openGapPercBelow', 'priceRangeAbove', 'priceRangeBelow', 'tradeCountAbove', 'tradeCountBelow', 'tradeRateAbove', 'tradeRateBelow', 'volumeRateAbove', 'volumeRateBelow', 'stVolume3minAbove', 'stVolume3minBelow', 'stVolume5minAbove', 'stVolume5minBelow', 'stVolume10minAbove', 'stVolume10minBelow', 'prodCatIs', 'bondUSStateLike', 'issuerCountryIs', 'bondStkSymbolIs', 'bondIssuerLike', 'bondCallableIs', 'unshortableIs', 'haltedIs', 'shortSaleRestrictionIs', 'sharesAvailableManyAbove', 'sharesAvailableManyBelow', 'feeRateAbove', 'feeRateBelow', 'rebateRateAbove', 'rebateRateBelow', 'utilizationAbove', 'utilizationBelow', 'bondDefaultedIs', 'bondTradingFlatIs', 'bondExchListedIs', 'bondFdicInsIs', 'bondVarCouponRateIs', 'bondInsuredIs', 'bondGeneralObligationIs', 'bondRevenueIs', 'bondSubjectToAmtIs', 'bondRefundedIs', 'bondNoFederalTaxIs', 'bondBankQualifiedIs', 'bondBuildAmericaIs', 'bondValidBidOrAskOnly', 'bondBidAbove', 'bondBidBelow', 'bondAskAbove', 'bondAskBelow', 'bondBidOrAskAbove', 'bondBidOrAskBelow', 'bondBidSizeValueAbove', 'bondBidSizeValueBelow', 'bondAskSizeValueAbove', 'bondAskSizeValueBelow', 'bondBidOrAskSizeValueAbove', 'bondBidOrAskSizeValueBelow', 'bondBidYieldAbove', 'bondBidYieldBelow', 'bondAskYieldAbove', 'bondAskYieldBelow', 'bondBidOrAskYieldAbove', 'bondBidOrAskYieldBelow', 'bondSpreadAbove', 'bondSpreadBelow', 'bondPaymentFreqIs', 'bondAmtOutstandingAbove', 'bondAmtOutstandingBelow', 'bondNextCallDateAbove', 'bondNextCallDateBelow', 'bondDurationAbove', 'bondDurationBelow', 'bondConvexityAbove', 'bondConvexityBelow', 'bondStkMarketCapAbove', 'bondStkMarketCapBelow', 'bondDebtOutstandingAbove', 'bondDebtOutstandingBelow', 'bondDebtOutstandingMuniAbove', 'bondDebtOutstandingMuniBelow', 'bondDebt2EquityRatioAbove', 'bondDebt2EquityRatioBelow', 'bondDebt2BookRatioAbove', 'bondDebt2BookRatioBelow', 'bondDebt2TanBookRatioAbove', 'bondDebt2TanBookRatioBelow', 'bondEquity2BookRatioAbove', 'bondEquity2BookRatioBelow', 'bondEquity2TanBookRatioAbove', 'bondEquity2TanBookRatioBelow', 'bondStructRelated', 'bondAssetSubTypeStrBeginsWithOneOf', 'underConID', 'socialSentimentNetAbove', 'socialSentimentNetBelow', 'socialSentimentActivityAbove', 'socialSentimentActivityBelow', 'etfDistribFreqIs', 'etfAssetsAbove', 'etfAssetsBelow', 'etfExpenseAbove', 'etfExpenseBelow', 'etfALTARAbove', 'etfALTARBelow', 'etfAvgALTARAbove', 'etfAvgALTARBelow', 'etfTRytdAbove', 'etfTRytdBelow', 'etfTR1yrAbove', 'etfTR1yrBelow', 'etfTR5yrAbove', 'etfTR5yrBelow', 'etfTR10yrAbove', 'etfTR10yrBelow', 'etfTRIncepAbove', 'etfTRIncepBelow', 'etfTrackingDiffPctAbove', 'etfTrackingDiffPctBelow', 'etfBidAskPctAbove', 'etfBidAskPctBelow', 'etfEmgAbove', 'etfEmgBelow', 'etfDevAbove', 'etfDevBelow', 'etf5yrEPSAbove', 'etf5yrEPSBelow', 'etfLeverageAbove', 'etfLeverageBelow', 'etfAssetTurnsAbove', 'etfAssetTurnsBelow', 'etf1moChngAbove', 'etf1moChngBelow', 'etf3moChngAbove', 'etf3moChngBelow', 'etfPayoutAbove', 'etfPayoutBelow', 'etfFwd_PEAbove', 'etfFwd_PEBelow', 'etfFwd_PCFAbove', 'etfFwd_PCFBelow', 'etfFwd_YldAbove', 'etfFwd_YldBelow', 'etfBetaSPXAbove', 'etfBetaSPXBelow', 'etfLTGAbove', 'etfLTGBelow', 'etfSht_IntAbove', 'etfSht_IntBelow', 'etfRSIAbove', 'etfRSIBelow', 'etfYTM_WgtAvgAbove', 'etfYTM_WgtAvgBelow', 'etfCPN_WgtAvgAbove', 'etfCPN_WgtAvgBelow', 'etfCurYld_WgtAvgAbove', 'etfCurYld_WgtAvgBelow', 'etfMaturity_WgtAvgAbove', 'etfMaturity_WgtAvgBelow', 'etfDUR_WgtAvgAbove', 'etfDUR_WgtAvgBelow', 'etfMOD_DUR_WgtAvgAbove', 'etfMOD_DUR_WgtAvgBelow', 'etfCONVEX_WgtAvgAbove', 'etfCONVEX_WgtAvgBelow', 'etfNum_DistinctAbove', 'etfNum_DistinctBelow', 'etfComponent_CountAbove', 'etfComponent_CountBelow', 'etfPct_FixedAbove', 'etfPct_FixedBelow', 'etfPct_at_MtyAbove', 'etfPct_at_MtyBelow', 'etfMoodyRatingAbove', 'etfMoodyRatingBelow', 'etfSPRatingAbove', 'etfSPRatingBelow', 'prevYrETFFYBVPerShareAbove', 'prevYrETFFYBVPerShareBelow', 'prevYrETFFYDividendYieldAbove', 'prevYrETFFYDividendYieldBelow', 'prevYrETFFYDPSAbove', 'prevYrETFFYDPSBelow', 'prevYrETFFYEPSAbove', 'prevYrETFFYEPSBelow', 'prevYrETFFYEPSGrowthAbove', 'prevYrETFFYEPSGrowthBelow', 'prevYrETFFYNetMarginAbove', 'prevYrETFFYNetMarginBelow', 'prevYrETFFYPriceToBookValueAbove', 'prevYrETFFYPriceToBookValueBelow', 'prevYrETFFYPriceToCashFlowAbove', 'prevYrETFFYPriceToCashFlowBelow', 'prevYrETFFYPriceToEarningsAbove', 'prevYrETFFYPriceToEarningsBelow', 'prevYrETFFYPriceToGrowthAbove', 'prevYrETFFYPriceToGrowthBelow', 'prevYrETFFYPriceToSalesAbove', 'prevYrETFFYPriceToSalesBelow', 'prevYrETFFYRoEAbove', 'prevYrETFFYRoEBelow', 'prevYrETFFYSPSAbove', 'prevYrETFFYSPSBelow', 'prevYrETFFYSPSGrowthAbove', 'prevYrETFFYSPSGrowthBelow', 'currYrETFFYBVPerShareAbove', 'currYrETFFYBVPerShareBelow', 'currYrETFFYDividendYieldAbove', 'currYrETFFYDividendYieldBelow', 'currYrETFFYDPSAbove', 'currYrETFFYDPSBelow', 'currYrETFFYEPSAbove', 'currYrETFFYEPSBelow', 'currYrETFFYEPSGrowthAbove', 'currYrETFFYEPSGrowthBelow', 'currYrETFFYNetMarginAbove', 'currYrETFFYNetMarginBelow', 'currYrETFFYPriceToBookValueAbove', 'currYrETFFYPriceToBookValueBelow', 'currYrETFFYPriceToCashFlowAbove', 'currYrETFFYPriceToCashFlowBelow', 'currYrETFFYPriceToEarningsAbove', 'currYrETFFYPriceToEarningsBelow', 'currYrETFFYPriceToGrowthAbove', 'currYrETFFYPriceToGrowthBelow', 'currYrETFFYPriceToSalesAbove', 'currYrETFFYPriceToSalesBelow', 'currYrETFFYRoEAbove', 'currYrETFFYRoEBelow', 'currYrETFFYSPSAbove', 'currYrETFFYSPSBelow', 'currYrETFFYSPSGrowthAbove', 'currYrETFFYSPSGrowthBelow', 'nextYrETFFYBVPerShareAbove', 'nextYrETFFYBVPerShareBelow', 'nextYrETFFYDividendYieldAbove', 'nextYrETFFYDividendYieldBelow', 'nextYrETFFYDPSAbove', 'nextYrETFFYDPSBelow', 'nextYrETFFYEPSAbove', 'nextYrETFFYEPSBelow', 'nextYrETFFYEPSGrowthAbove', 'nextYrETFFYEPSGrowthBelow', 'nextYrETFFYNetMarginAbove', 'nextYrETFFYNetMarginBelow', 'nextYrETFFYPriceToBookValueAbove', 'nextYrETFFYPriceToBookValueBelow', 'nextYrETFFYPriceToCashFlowAbove', 'nextYrETFFYPriceToCashFlowBelow', 'nextYrETFFYPriceToEarningsAbove', 'nextYrETFFYPriceToEarningsBelow', 'nextYrETFFYPriceToGrowthAbove', 'nextYrETFFYPriceToGrowthBelow', 'nextYrETFFYPriceToSalesAbove', 'nextYrETFFYPriceToSalesBelow', 'nextYrETFFYRoEAbove', 'nextYrETFFYRoEBelow', 'nextYrETFFYSPSAbove', 'nextYrETFFYSPSBelow', 'nextYrETFFYSPSGrowthAbove', 'nextYrETFFYSPSGrowthBelow', 'btOptObj', 'btOptVendor', 'btNotional', 'btLeverageLong', 'btLeverageShort', 'btIndex', 'researchProviderIs', 'btLongPosTopBottom', 'btLongPosRank', 'btShortPosTopBottom', 'btShortPosRank', 'btWeight', 'Rebalance', 'btRebalanceFrequency', 'btTestPeriod', 'btBenchmark', 'sort_weights', 'street_ratingsAbove', 'street_ratingsBelow', 'valuengineAbove', 'valuengineBelow', 'sadif_analyticsAbove', 'sadif_analyticsBelow', 'market_edgeAbove', 'market_edgeBelow', 'validea_guruAbove', 'validea_guruBelow', 'market_graderAbove', 'market_graderBelow', 'morningstarAbove', 'morningstarBelow', 'constructs_researchAbove', 'constructs_researchBelow', 'zacksAbove', 'zacksBelow', 'shortInterestChgAbove', 'shortInterestChgBelow', 'shortOutstandingRatioAbove', 'shortOutstandingRatioBelow', 'reutEPSChgPct_TTMAbove', 'reutEPSChgPct_TTMBelow', 'reutPENorm_MRYAbove', 'reutPENorm_MRYBelow', 'reutPayoutRatioPct_TTMAbove', 'reutPayoutRatioPct_TTMBelow', 'reutRev_TTMAbove', 'reutRev_TTMBelow', 'reutEBT_TTMAbove', 'reutEBT_TTMBelow', 'reutTotDebtTotEquityRatio_MRQ_Above', 'reutTotDebtTotEquityRatio_MRQ_Below', 'reutRevChgPct_TTMAbove', 'reutRevChgPct_TTMBelow', 'reutPrice2CashFlowPerShare_TTMAbove', 'reutPrice2CashFlowPerShare_TTMBelow', 'reutCurrentRatio_MRQAbove', 'reutCurrentRatio_MRQBelow', 'reutLongTermDebt2Equity_MRQAbove', 'reutLongTermDebt2Equity_MRQBelow', 'reutNetIncomeAvlble2Common_TTMAbove', 'reutNetIncomeAvlble2Common_TTMBelow', 'reutNetProfitMarginPct_TTMAbove', 'reutNetProfitMarginPct_TTMBelow', 'reutRevChgPctAbove', 'reutRevChgPctBelow', 'reutPrice2SalesRatio_TTMAbove', 'reutPrice2SalesRatio_TTMBelow', 'reutGrossMargin_TTMAbove', 'reutGrossMargin_TTMBelow', 'reutEBTNorm_MRYAbove', 'reutEBTNorm_MRYBelow', 'reutNetIncomeAvlble2CommonNorm_MRYAbove', 'reutNetIncomeAvlble2CommonNorm_MRYBelow', 'reutReturnOnAvgAssetsPct_TTMAbove', 'reutReturnOnAvgAssetsPct_TTMBelow', 'reutReturnOnInvestmentPct_TTMAbove', 'reutReturnOnInvestmentPct_TTMBelow', 'reutOperatingMargin_TTMAbove', 'reutOperatingMargin_TTMBelow', 'reutEPSChgPctAbove', 'reutEPSChgPctBelow', 'reutPretaxMarginPct_MRYAbove', 'reutPretaxMarginPct_MRYBelow', 'reutEBITD_TTMAbove', 'reutEBITD_TTMBelow', 'ibPriceYTDPctAbove', 'ibPriceYTDPctBelow', 'ibFrac52wkAbove', 'ibFrac52wkBelow', 'reutPretaxMargin_TTMAbove', 'reutPretaxMargin_TTMBelow', 'reutFreeCashFlow_TTMAbove', 'reutFreeCashFlow_TTMBelow', 'reutNetDebtIAbove', 'reutNetDebtIBelow', 'reutBetaAbove', 'reutBetaBelow', 'ibPriceYTDPctR_Above', 'ibPriceYTDPctR_Below', 'reutEnterpriseValueCurAbove', 'reutEnterpriseValueCurBelow', 'curEMA20Above', 'curEMA20Below', 'curEMA50Above', 'curEMA50Below', 'curEMA100Above', 'curEMA100Below', 'curEMA200Above', 'curEMA200Below', 'lastVsEMAChangeRatio20Above', 'lastVsEMAChangeRatio20Below', 'lastVsEMAChangeRatio50Above', 'lastVsEMAChangeRatio50Below', 'lastVsEMAChangeRatio100Above', 'lastVsEMAChangeRatio100Below', 'lastVsEMAChangeRatio200Above', 'lastVsEMAChangeRatio200Below', 'curMACDAbove', 'curMACDBelow', 'curMACDSignalAbove', 'curMACDSignalBelow', 'curMACDDistAbove', 'curMACDDistBelow', 'curPPOAbove', 'curPPOBelow', 'curPPOSignalAbove', 'curPPOSignalBelow', 'curPPODistAbove', 'curPPODistBelow', 'reutCurrentAssets_MRQAbove', 'reutCurrentAssets_MRQBelow', 'reutCurrentAssets_MRYAbove', 'reutCurrentAssets_MRYBelow', 'reutCashFlowPerShare_MRYAbove', 'reutCashFlowPerShare_MRYBelow', 'reutEBIT_MRQAbove', 'reutEBIT_MRQBelow', 'reutEBIT_MRYAbove', 'reutEBIT_MRYBelow', 'reutEPS_MRYAbove', 'reutEPS_MRYBelow', 'reutIssuanceOfDebt_MRQAbove', 'reutIssuanceOfDebt_MRQBelow', 'reutIssuanceOfDebt_MRYAbove', 'reutIssuanceOfDebt_MRYBelow', 'reutIssuanceOfStock_MRQAbove', 'reutIssuanceOfStock_MRQBelow', 'reutIssuanceOfStock_MRYAbove', 'reutIssuanceOfStock_MRYBelow', 'reutShortTermDebt_MRQAbove', 'reutShortTermDebt_MRQBelow', 'reutShortTermDebt_MRYAbove', 'reutShortTermDebt_MRYBelow', 'reutCurrentLiabilities_MRQAbove', 'reutCurrentLiabilities_MRQBelow', 'reutCurrentLiabilities_MRYAbove', 'reutCurrentLiabilities_MRYBelow', 'reutCashFromOperating_MRQAbove', 'reutCashFromOperating_MRQBelow', 'reutCashFromOperating_MRYAbove', 'reutCashFromOperating_MRYBelow', 'reutRevenuePerShare_MRYAbove', 'reutRevenuePerShare_MRYBelow', 'reutReturnOnAvgAssetsPct_MRYAbove', 'reutReturnOnAvgAssetsPct_MRYBelow', 'reutReturnOnInvestmentPct_MRYAbove', 'reutReturnOnInvestmentPct_MRYBelow', 'reutCapitalExpenditures_MRQAbove', 'reutCapitalExpenditures_MRQBelow', 'reutCapitalExpenditures_MRYAbove', 'reutCapitalExpenditures_MRYBelow', 'reutCashFromFinancing_MRQAbove', 'reutCashFromFinancing_MRQBelow', 'reutCashFromFinancing_MRYAbove', 'reutCashFromFinancing_MRYBelow', 'reutCashFromInvesting_MRQAbove', 'reutCashFromInvesting_MRQBelow', 'reutCashFromInvesting_MRYAbove', 'reutCashFromInvesting_MRYBelow', 'reutInterestExpense_MRQAbove', 'reutInterestExpense_MRQBelow', 'reutInterestExpense_MRYAbove', 'reutInterestExpense_MRYBelow', 'reutOperatingIncome_MRQAbove', 'reutOperatingIncome_MRQBelow', 'reutOperatingIncome_MRYAbove', 'reutOperatingIncome_MRYBelow', 'reutEnterpriseValueToEBITDAAbove', 'reutEnterpriseValueToEBITDABelow', 'ibPrice1WKPctAbove', 'ibPrice1WKPctBelow', 'ibPrice4WKPctAbove', 'ibPrice4WKPctBelow', 'ibPrice13WKPctAbove', 'ibPrice13WKPctBelow', 'ibPrice52WKPctAbove', 'ibPrice52WKPctBelow', 'reutYieldAbove', 'reutYieldBelow', 'reutCash_MRQAbove', 'reutCash_MRQBelow', 'reutEBITDA_MRQAbove', 'reutEBITDA_MRQBelow', 'reutPrice2SalesRatio_MRQAbove', 'reutPrice2SalesRatio_MRQBelow', 'reutPrice2CashFlowPerShare_MRQAbove', 'reutPrice2CashFlowPerShare_MRQBelow', 'reutTotalAssets_MRQAbove', 'reutTotalAssets_MRQBelow', 'reutTotalLiabilities_MRQAbove', 'reutTotalLiabilities_MRQBelow', 'reutTotalCommonEquity_MRQAbove', 'reutTotalCommonEquity_MRQBelow', 'reutTotalLongTermDebt_MRQAbove', 'reutTotalLongTermDebt_MRQBelow', 'reutBookValueTanPerShare_MRQAbove', 'reutBookValueTanPerShare_MRQBelow', 'reutCashFlowPerShare_TTMAbove', 'reutCashFlowPerShare_TTMBelow', 'reutEPS_TTMAbove', 'reutEPS_TTMBelow', 'reutRev2Share_TTMAbove', 'reutRev2Share_TTMBelow', 'sharesOutstandingAbove', 'sharesOutstandingBelow', 'esgScoreAbove', 'esgScoreBelow', 'esgCombinedScoreAbove', 'esgCombinedScoreBelow', 'esgControversiesScoreAbove', 'esgControversiesScoreBelow', 'esgResourceUseScoreAbove', 'esgResourceUseScoreBelow', 'esgEmissionsScoreAbove', 'esgEmissionsScoreBelow', 'esgEnvInvScoreAbove', 'esgEnvInvScoreBelow', 'esgWorkforceScoreAbove', 'esgWorkforceScoreBelow', 'esgHrScoreAbove', 'esgHrScoreBelow', 'esgCommunityScoreAbove', 'esgCommunityScoreBelow', 'esgProdRespScoreAbove', 'esgProdRespScoreBelow', 'esgManagementScoreAbove', 'esgManagementScoreBelow', 'esgShareholdersScoreAbove', 'esgShareholdersScoreBelow', 'esgStrategyScoreAbove', 'esgStrategyScoreBelow', 'esgEnvPillarScoreAbove', 'esgEnvPillarScoreBelow', 'esgSocialPillarScoreAbove', 'esgSocialPillarScoreBelow', 'esgCorpGovPillarScoreAbove', 'esgCorpGovPillarScoreBelow', 'ivRank13wAbove', 'ivRank13wBelow', 'ivRank26wAbove', 'ivRank26wBelow', 'ivRank52wAbove', 'ivRank52wBelow', 'ivPercntl13wAbove', 'ivPercntl13wBelow', 'ivPercntl26wAbove', 'ivPercntl26wBelow', 'ivPercntl52wAbove', 'ivPercntl52wBelow', 'hvRank13wAbove', 'hvRank13wBelow', 'HVRank26wAbove', 'HVRank26wBelow', 'HVRank52wAbove', 'HVRank52wBelow', 'HVPercntl13wAbove', 'HVPercntl13wBelow', 'HVPercntl26wAbove', 'HVPercntl26wBelow', 'HVPercntl52wAbove', 'HVPercntl52wBelow', 'mfTotalReturnScoreOverallAbove', 'mfTotalReturnScoreOverallBelow', 'mfConsistentReturnScoreOverallAbove', 'mfConsistentReturnScoreOverallBelow', 'mfPreservationScoreOverallAbove', 'mfPreservationScoreOverallBelow', 'mfTaxEfficiencyScoreOverallAbove', 'mfTaxEfficiencyScoreOverallBelow', 'mfExpenseScoreOverallAbove', 'mfExpenseScoreOverallBelow', 'mfTotalReturnScore3yrAbove', 'mfTotalReturnScore3yrBelow', 'mfConsistentReturnScore3yrAbove', 'mfConsistentReturnScore3yrBelow', 'mfPreservationScore3yrAbove', 'mfPreservationScore3yrBelow', 'mfTaxEfficiencyScore3yrAbove', 'mfTaxEfficiencyScore3yrBelow', 'mfExpenseScore3yrAbove', 'mfExpenseScore3yrBelow', 'mfTotalReturnScore5yrAbove', 'mfTotalReturnScore5yrBelow', 'mfConsistentReturnScore5yrAbove', 'mfConsistentReturnScore5yrBelow', 'mfPreservationScore5yrAbove', 'mfPreservationScore5yrBelow', 'mfTaxEfficiencyScore5yrAbove', 'mfTaxEfficiencyScore5yrBelow', 'mfExpenseScore5yrAbove', 'mfExpenseScore5yrBelow', 'mfTotalReturnScore10yrAbove', 'mfTotalReturnScore10yrBelow', 'mfConsistentReturnScore10yrAbove', 'mfConsistentReturnScore10yrBelow', 'mfPreservationScore10yrAbove', 'mfPreservationScore10yrBelow', 'mfTaxEfficiencyScore10yrAbove', 'mfTaxEfficiencyScore10yrBelow', 'mfExpenseScore10yrAbove', 'mfExpenseScore10yrBelow', 'mfDistributionYield1yrValueAbove', 'mfDistributionYield1yrValueBelow', 'mfPercentageGrowthCumulativeValueAbove', 'mfPercentageGrowthCumulativeValueBelow', 'mfAnnualizedPerformance3yrValueAbove', 'mfAnnualizedPerformance3yrValueBelow', 'mfAnnualizedPerformance5yrValueAbove', 'mfAnnualizedPerformance5yrValueBelow', 'mfAnnualizedPerformance10yrValueAbove', 'mfAnnualizedPerformance10yrValueBelow', 'mfPriceChangeValueAbove', 'mfPriceChangeValueBelow', 'mfYieldValueAbove', 'mfYieldValueBelow', 'mfProjectedYieldValueAbove', 'mfProjectedYieldValueBelow', 'mfPctChangeAbove', 'mfPctChangeBelow', 'mfAdjustedRSquared1yrAbove', 'mfAdjustedRSquared1yrBelow', 'mfAlpha1yrAbove', 'mfAlpha1yrBelow', 'mfAverageLoss1yrAbove', 'mfAverageLoss1yrBelow', 'mfAverageReturn1yrAbove', 'mfAverageReturn1yrBelow', 'mfBearBeta1yrAbove', 'mfBearBeta1yrBelow', 'mfBeta1yrAbove', 'mfBeta1yrBelow', 'mfBullBeta1yrAbove', 'mfBullBeta1yrBelow', 'mfCovariance1yrAbove', 'mfCovariance1yrBelow', 'mfCorrelation1yrAbove', 'mfCorrelation1yrBelow', 'mfDownsideDeviationPopulation1yrAbove', 'mfDownsideDeviationPopulation1yrBelow', 'mfInformationRatio1yrAbove', 'mfInformationRatio1yrBelow', 'mfMaxGain1yrAbove', 'mfMaxGain1yrBelow', 'mfMaxLoss1yrAbove', 'mfMaxLoss1yrBelow', 'mfMaximumDrawdown1yrAbove', 'mfMaximumDrawdown1yrBelow', 'mfPositivePeriods1yrAbove', 'mfPositivePeriods1yrBelow', 'mfRSquared1yrAbove', 'mfRSquared1yrBelow', 'mfReturnRiskRatio1yrAbove', 'mfReturnRiskRatio1yrBelow', 'mfSRRI1yrAbove', 'mfSRRI1yrBelow', 'mfSemiDeviation1yrAbove', 'mfSemiDeviation1yrBelow', 'mfSemiVariance1yrAbove', 'mfSemiVariance1yrBelow', 'mfSharpeRatio1yrAbove', 'mfSharpeRatio1yrBelow', 'mfSortino1yrAbove', 'mfSortino1yrBelow', 'mfStandardDeviation1yrAbove', 'mfStandardDeviation1yrBelow', 'mfTrackingError1yrAbove', 'mfTrackingError1yrBelow', 'mfTreynorRatio1yrAbove', 'mfTreynorRatio1yrBelow', 'mfValueAtRiskNormal1yrAbove', 'mfValueAtRiskNormal1yrBelow', 'mfValueAtRiskNormalETL1yrAbove', 'mfValueAtRiskNormalETL1yrBelow', 'mfValueAtRiskQuantile1yrAbove', 'mfValueAtRiskQuantile1yrBelow', 'mfValueAtRiskQuantileETL1yrAbove', 'mfValueAtRiskQuantileETL1yrBelow', 'mfVariance1yrAbove', 'mfVariance1yrBelow', 'mfNumberOfSecuritiesAbove', 'mfNumberOfSecuritiesBelow', 'mfDividendPayoutRatioAbove', 'mfDividendPayoutRatioBelow', 'mfDividendPayoutRatio5yrAbove', 'mfDividendPayoutRatio5yrBelow', 'mfDividendPerShare1yrAbove', 'mfDividendPerShare1yrBelow', 'mfDividendPerShare3yrAbove', 'mfDividendPerShare3yrBelow', 'mfPrice2DividendAbove', 'mfPrice2DividendBelow', 'mfDividendYieldWAvgAbove', 'mfDividendYieldWAvgBelow', 'mfEBIT2InterestAbove', 'mfEBIT2InterestBelow', 'mfMktCapAvgLatestAbove', 'mfMktCapAvgLatestBelow', 'mfOperCashFlowGrowthRate3yrAbove', 'mfOperCashFlowGrowthRate3yrBelow', 'mfSalesGrowth1yrAbove', 'mfSalesGrowth1yrBelow', 'mfSalesGrowth3yrAbove', 'mfSalesGrowth3yrBelow', 'mfSalesGrowth5yrAbove', 'mfSalesGrowth5yrBelow', 'mfLatestPrice2EarningsRatioAbove', 'mfLatestPrice2EarningsRatioBelow', 'mfLatestPrice2BookRatioAbove', 'mfLatestPrice2BookRatioBelow', 'mfLatestPrice2SalesRatioAbove', 'mfLatestPrice2SalesRatioBelow', 'mfLatestReturnOnEquityWAvgAbove', 'mfLatestReturnOnEquityWAvgBelow', 'mfLatestSalesPerShareGrowth3yrAbove', 'mfLatestSalesPerShareGrowth3yrBelow', 'mfLTDebt2ShareholdersEquityAbove', 'mfLTDebt2ShareholdersEquityBelow', 'mfEPSGrowth1yrAbove', 'mfEPSGrowth1yrBelow', 'mfEPSGrowth3yrAbove', 'mfEPSGrowth3yrBelow', 'mfEPSGrowth5yrAbove', 'mfEPSGrowth5yrBelow', 'mfPrice2EarningsRatioAbove', 'mfPrice2EarningsRatioBelow', 'mfPrice2BookRatioAbove', 'mfPrice2BookRatioBelow', 'mfPrice2SalesRatioAbove', 'mfPrice2SalesRatioBelow', 'mfReturnOnAssets1yrAbove', 'mfReturnOnAssets1yrBelow', 'mfReturnOnAssets3yrAbove', 'mfReturnOnAssets3yrBelow', 'mfReturnOnEquity1yrAbove', 'mfReturnOnEquity1yrBelow', 'mfReturnOnEquity3yrAbove', 'mfReturnOnEquity3yrBelow', 'mfReturnOnInvestment1yrAbove', 'mfReturnOnInvestment1yrBelow', 'mfReturnOnInvestment3yrAbove', 'mfReturnOnInvestment3yrBelow', 'mfSales2TotalAssetsAbove', 'mfSales2TotalAssetsBelow', 'mfSalesPerShareGrowth1yrAbove', 'mfSalesPerShareGrowth1yrBelow', 'mfSalesPerShareGrowth3yrAbove', 'mfSalesPerShareGrowth3yrBelow', 'mfTotalAssets2TotalEquityAbove', 'mfTotalAssets2TotalEquityBelow', 'mfTotalDebt2TotalCapitalAbove', 'mfTotalDebt2TotalCapitalBelow', 'mfTotalDebt2TotalEquityAbove', 'mfTotalDebt2TotalEquityBelow', 'mfPrice2CashAbove', 'mfPrice2CashBelow', 'mfRelativeStrengthAbove', 'mfRelativeStrengthBelow', 'mfReturnOnCapitalAbove', 'mfReturnOnCapitalBelow', 'mfReturnOnCapital3yrAbove', 'mfReturnOnCapital3yrBelow', 'mfLatestCompositeZScoreAbove', 'mfLatestCompositeZScoreBelow', 'mfLatestPrice2EarningsZScoreAbove', 'mfLatestPrice2EarningsZScoreBelow', 'mfLatestPrice2BookZScoreAbove', 'mfLatestPrice2BookZScoreBelow', 'mfLatestPrice2SalesZScoreAbove', 'mfLatestPrice2SalesZScoreBelow', 'mfLatestSPSGrowthZScoreAbove', 'mfLatestSPSGrowthZScoreBelow', 'mfAverageFinalCompositeZScoreAbove', 'mfAverageFinalCompositeZScoreBelow', 'mfWeightedFinalCompositeZScoreAbove', 'mfWeightedFinalCompositeZScoreBelow', 'mfDividendYieldZScoreAbove', 'mfDividendYieldZScoreBelow', 'mfReturnOnEquityZScoreAbove', 'mfReturnOnEquityZScoreBelow', 'mfYieldToMaturityAbove', 'mfYieldToMaturityBelow', 'mfNominalMaturityAbove', 'mfNominalMaturityBelow', 'mfEffectiveMaturityAbove', 'mfEffectiveMaturityBelow', 'mfAverageCouponAbove', 'mfAverageCouponBelow', 'mfCalculatedAverageQualityAbove', 'mfCalculatedAverageQualityBelow', 'bidAskPriceType', 'seperatorType', 'seperatorTypeSym', 'bidAskPriceType', 'seperatorType', 'seperatorTypeSym', 'bidAskPriceType', 'seperatorType', 'seperatorTypeSym', 'bidAskPriceType', 'seperatorType', 'seperatorTypeSym', 'bidAskPriceType', 'seperatorType', 'seperatorTypeSym', 'bidAskPriceType', 'seperatorType', 'seperatorTypeSym', 'btFilteringCriteria', 'reutEBTNorm_MRYAbove', 'reutEBTNorm_MRYBelow', 'reutNetIncomeAvlble2CommonNorm_MRYAbove', 'reutNetIncomeAvlble2CommonNorm_MRYBelow', 'reutPENorm_MRYAbove', 'reutPENorm_MRYBelow', 'reutPretaxMarginPct_MRYAbove', 'reutPretaxMarginPct_MRYBelow', 'avgAnalystTarget2PriceRatioAbove', 'avgAnalystTarget2PriceRatioBelow', 'avgPriceTargetAbove', 'avgPriceTargetBelow', 'avgRatingAbove', 'avgRatingBelow', 'reutEPSChgPctAbove', 'reutEPSChgPctBelow', 'histDividendFrdYieldAbove', 'histDividendFrdYieldBelow', 'reutLongTermDebt2Equity_MRQAbove', 'reutLongTermDebt2Equity_MRQBelow', 'reutPrice2SalesRatio_TTMAbove', 'reutPrice2SalesRatio_TTMBelow', 'reutReturnOnInvestmentPct_TTMAbove', 'reutReturnOnInvestmentPct_TTMBelow', 'ibFrac52wkAbove', 'ibFrac52wkBelow', 'ihInsiderOfFloatPercAbove', 'ihInsiderOfFloatPercBelow', 'ihNumSharesInsiderAbove', 'ihNumSharesInsiderBelow', 'iiNumSharesInstitutionalAbove', 'iiNumSharesInstitutionalBelow', 'numPriceTargetsAbove', 'numPriceTargetsBelow', 'numRatingsAbove', 'numRatingsBelow', 'minPeRatio', 'maxPeRatio', 'minPrice2TanBk', 'maxPrice2TanBk', 'minPrice2Bk', 'maxPrice2Bk', 'reutCurrentRatio_MRQAbove', 'reutCurrentRatio_MRQBelow', 'minQuickRatio', 'maxQuickRatio', 'reutRevChgPctAbove', 'reutRevChgPctBelow', 'reutEPSChgPct_TTMAbove', 'reutEPSChgPct_TTMBelow', 'reutEBITD_TTMAbove', 'reutEBITD_TTMBelow', 'reutEBT_TTMAbove', 'reutEBT_TTMBelow', 'reutGrossMargin_TTMAbove', 'reutGrossMargin_TTMBelow', 'reutNetIncomeAvlble2Common_TTMAbove', 'reutNetIncomeAvlble2Common_TTMBelow', 'reutNetProfitMarginPct_TTMAbove', 'reutNetProfitMarginPct_TTMBelow', 'reutOperatingMargin_TTMAbove', 'reutOperatingMargin_TTMBelow', 'reutPayoutRatioPct_TTMAbove', 'reutPayoutRatioPct_TTMBelow', 'reutPrice2CashFlowPerShare_TTMAbove', 'reutPrice2CashFlowPerShare_TTMBelow', 'reutRev_TTMAbove', 'reutRev_TTMBelow', 'reutRevChgPct_TTMAbove', 'reutRevChgPct_TTMBelow', 'reutReturnOnAvgAssetsPct_TTMAbove', 'reutReturnOnAvgAssetsPct_TTMBelow', 'minRetnOnEq', 'maxRetnOnEq', 'ibPriceYTDPctAbove', 'ibPriceYTDPctBelow', 'reutPretaxMargin_TTMAbove', 'reutPretaxMargin_TTMBelow', 'reutFreeCashFlow_TTMAbove', 'reutFreeCashFlow_TTMBelow', 'reutNetDebtIAbove', 'reutNetDebtIBelow', 'shortInterestChgAbove', 'shortInterestChgBelow', 'shortOutstandingRatioAbove', 'shortOutstandingRatioBelow', 'market_edgeAbove', 'market_edgeBelow', 'market_graderAbove', 'market_graderBelow', 'morningstarAbove', 'morningstarBelow', 'constructs_researchAbove', 'constructs_researchBelow', 'sadif_analyticsAbove', 'sadif_analyticsBelow', 'street_ratingsAbove', 'street_ratingsBelow', 'validea_guruAbove', 'validea_guruBelow', 'valuengineAbove', 'valuengineBelow', 'zacksAbove', 'zacksBelow', 'marketCapAbove1e6', 'marketCapBelow1e6', 'reutEnterpriseValueCurAbove', 'reutEnterpriseValueCurBelow', 'ibPriceYTDPctR_Above', 'ibPriceYTDPctR_Below', 'reutTotDebtTotEquityRatio_MRQ_Above', 'reutTotDebtTotEquityRatio_MRQ_Below', 'reutCurrentAssets_MRQAbove', 'reutCurrentAssets_MRQBelow', 'reutCurrentAssets_MRYAbove', 'reutCurrentAssets_MRYBelow', 'reutCashFlowPerShare_MRYAbove', 'reutCashFlowPerShare_MRYBelow', 'reutEBIT_MRQAbove', 'reutEBIT_MRQBelow', 'reutEBIT_MRYAbove', 'reutEBIT_MRYBelow', 'reutEPS_MRYAbove', 'reutEPS_MRYBelow', 'reutIssuanceOfDebt_MRQAbove', 'reutIssuanceOfDebt_MRQBelow', 'reutIssuanceOfDebt_MRYAbove', 'reutIssuanceOfDebt_MRYBelow', 'reutIssuanceOfStock_MRQAbove', 'reutIssuanceOfStock_MRQBelow', 'reutIssuanceOfStock_MRYAbove', 'reutIssuanceOfStock_MRYBelow', 'reutShortTermDebt_MRQAbove', 'reutShortTermDebt_MRQBelow', 'reutShortTermDebt_MRYAbove', 'reutShortTermDebt_MRYBelow', 'reutCurrentLiabilities_MRQAbove', 'reutCurrentLiabilities_MRQBelow', 'reutCurrentLiabilities_MRYAbove', 'reutCurrentLiabilities_MRYBelow', 'reutCashFromOperating_MRQAbove', 'reutCashFromOperating_MRQBelow', 'reutCashFromOperating_MRYAbove', 'reutCashFromOperating_MRYBelow', 'reutRevenuePerShare_MRYAbove', 'reutRevenuePerShare_MRYBelow', 'reutReturnOnAvgAssetsPct_MRYAbove', 'reutReturnOnAvgAssetsPct_MRYBelow', 'reutReturnOnInvestmentPct_MRYAbove', 'reutReturnOnInvestmentPct_MRYBelow', 'reutCapitalExpenditures_MRQAbove', 'reutCapitalExpenditures_MRQBelow', 'reutCapitalExpenditures_MRYAbove', 'reutCapitalExpenditures_MRYBelow', 'reutCashFromFinancing_MRQAbove', 'reutCashFromFinancing_MRQBelow', 'reutCashFromFinancing_MRYAbove', 'reutCashFromFinancing_MRYBelow', 'reutCashFromInvesting_MRQAbove', 'reutCashFromInvesting_MRQBelow', 'reutCashFromInvesting_MRYAbove', 'reutCashFromInvesting_MRYBelow', 'reutInterestExpense_MRQAbove', 'reutInterestExpense_MRQBelow', 'reutInterestExpense_MRYAbove', 'reutInterestExpense_MRYBelow', 'reutOperatingIncome_MRQAbove', 'reutOperatingIncome_MRQBelow', 'reutOperatingIncome_MRYAbove', 'reutOperatingIncome_MRYBelow', 'reutEnterpriseValueToEBITDAAbove', 'reutEnterpriseValueToEBITDABelow', 'ibPrice1WKPctAbove', 'ibPrice1WKPctBelow', 'ibPrice4WKPctAbove', 'ibPrice4WKPctBelow', 'ibPrice13WKPctAbove', 'ibPrice13WKPctBelow', 'ibPrice52WKPctAbove', 'ibPrice52WKPctBelow', 'reutYieldAbove', 'reutYieldBelow', 'reutCash_MRQAbove', 'reutCash_MRQBelow', 'reutEBITDA_MRQAbove', 'reutEBITDA_MRQBelow', 'reutPrice2SalesRatio_MRQAbove', 'reutPrice2SalesRatio_MRQBelow', 'reutPrice2CashFlowPerShare_MRQAbove', 'reutPrice2CashFlowPerShare_MRQBelow', 'reutTotalAssets_MRQAbove', 'reutTotalAssets_MRQBelow', 'reutTotalLiabilities_MRQAbove', 'reutTotalLiabilities_MRQBelow', 'reutTotalCommonEquity_MRQAbove', 'reutTotalCommonEquity_MRQBelow', 'reutTotalLongTermDebt_MRQAbove', 'reutTotalLongTermDebt_MRQBelow', 'reutBookValueTanPerShare_MRQAbove', 'reutBookValueTanPerShare_MRQBelow', 'reutCashFlowPerShare_TTMAbove', 'reutCashFlowPerShare_TTMBelow', 'reutEPS_TTMAbove', 'reutEPS_TTMBelow', 'reutRev2Share_TTMAbove', 'reutRev2Share_TTMBelow', 'sharesOutstandingAbove', 'sharesOutstandingBelow', 'btFundamentalsCriteria', 'reutEBTNorm_MRYAbove', 'reutEBTNorm_MRYBelow', 'reutNetIncomeAvlble2CommonNorm_MRYAbove', 'reutNetIncomeAvlble2CommonNorm_MRYBelow', 'reutPENorm_MRYAbove', 'reutPENorm_MRYBelow', 'reutPretaxMarginPct_MRYAbove', 'reutPretaxMarginPct_MRYBelow', 'avgAnalystTarget2PriceRatioAbove', 'avgAnalystTarget2PriceRatioBelow', 'avgPriceTargetAbove', 'avgPriceTargetBelow', 'avgRatingAbove', 'avgRatingBelow', 'reutEPSChgPctAbove', 'reutEPSChgPctBelow', 'histDividendFrdYieldAbove', 'histDividendFrdYieldBelow', 'reutLongTermDebt2Equity_MRQAbove', 'reutLongTermDebt2Equity_MRQBelow', 'reutPrice2SalesRatio_TTMAbove', 'reutPrice2SalesRatio_TTMBelow', 'reutReturnOnInvestmentPct_TTMAbove', 'reutReturnOnInvestmentPct_TTMBelow', 'ibFrac52wkAbove', 'ibFrac52wkBelow', 'ihInsiderOfFloatPercAbove', 'ihInsiderOfFloatPercBelow', 'ihNumSharesInsiderAbove', 'ihNumSharesInsiderBelow', 'iiNumSharesInstitutionalAbove', 'iiNumSharesInstitutionalBelow', 'numPriceTargetsAbove', 'numPriceTargetsBelow', 'numRatingsAbove', 'numRatingsBelow', 'minPeRatio', 'maxPeRatio', 'minPrice2TanBk', 'maxPrice2TanBk', 'minPrice2Bk', 'maxPrice2Bk', 'reutCurrentRatio_MRQAbove', 'reutCurrentRatio_MRQBelow', 'minQuickRatio', 'maxQuickRatio', 'reutRevChgPctAbove', 'reutRevChgPctBelow', 'reutEPSChgPct_TTMAbove', 'reutEPSChgPct_TTMBelow', 'reutEBITD_TTMAbove', 'reutEBITD_TTMBelow', 'reutEBT_TTMAbove', 'reutEBT_TTMBelow', 'reutGrossMargin_TTMAbove', 'reutGrossMargin_TTMBelow', 'reutNetIncomeAvlble2Common_TTMAbove', 'reutNetIncomeAvlble2Common_TTMBelow', 'reutNetProfitMarginPct_TTMAbove', 'reutNetProfitMarginPct_TTMBelow', 'reutOperatingMargin_TTMAbove', 'reutOperatingMargin_TTMBelow', 'reutPayoutRatioPct_TTMAbove', 'reutPayoutRatioPct_TTMBelow', 'reutPrice2CashFlowPerShare_TTMAbove', 'reutPrice2CashFlowPerShare_TTMBelow', 'reutRev_TTMAbove', 'reutRev_TTMBelow', 'reutRevChgPct_TTMAbove', 'reutRevChgPct_TTMBelow', 'reutReturnOnAvgAssetsPct_TTMAbove', 'reutReturnOnAvgAssetsPct_TTMBelow', 'minRetnOnEq', 'maxRetnOnEq', 'ibPriceYTDPctAbove', 'ibPriceYTDPctBelow', 'reutPretaxMargin_TTMAbove', 'reutPretaxMargin_TTMBelow', 'reutFreeCashFlow_TTMAbove', 'reutFreeCashFlow_TTMBelow', 'reutNetDebtIAbove', 'reutNetDebtIBelow', 'shortInterestChgAbove', 'shortInterestChgBelow', 'shortOutstandingRatioAbove', 'shortOutstandingRatioBelow', 'market_edgeAbove', 'market_edgeBelow', 'market_graderAbove', 'market_graderBelow', 'morningstarAbove', 'morningstarBelow', 'constructs_researchAbove', 'constructs_researchBelow', 'sadif_analyticsAbove', 'sadif_analyticsBelow', 'street_ratingsAbove', 'street_ratingsBelow', 'validea_guruAbove', 'validea_guruBelow', 'valuengineAbove', 'valuengineBelow', 'zacksAbove', 'zacksBelow', 'marketCapAbove1e6', 'marketCapBelow1e6', 'reutEnterpriseValueCurAbove', 'reutEnterpriseValueCurBelow', 'ibPriceYTDPctR_Above', 'ibPriceYTDPctR_Below', 'reutTotDebtTotEquityRatio_MRQ_Above', 'reutTotDebtTotEquityRatio_MRQ_Below', 'reutCurrentAssets_MRQAbove', 'reutCurrentAssets_MRQBelow', 'reutCurrentAssets_MRYAbove', 'reutCurrentAssets_MRYBelow', 'reutCashFlowPerShare_MRYAbove', 'reutCashFlowPerShare_MRYBelow', 'reutEBIT_MRQAbove', 'reutEBIT_MRQBelow', 'reutEBIT_MRYAbove', 'reutEBIT_MRYBelow', 'reutEPS_MRYAbove', 'reutEPS_MRYBelow', 'reutIssuanceOfDebt_MRQAbove', 'reutIssuanceOfDebt_MRQBelow', 'reutIssuanceOfDebt_MRYAbove', 'reutIssuanceOfDebt_MRYBelow', 'reutIssuanceOfStock_MRQAbove', 'reutIssuanceOfStock_MRQBelow', 'reutIssuanceOfStock_MRYAbove', 'reutIssuanceOfStock_MRYBelow', 'reutShortTermDebt_MRQAbove', 'reutShortTermDebt_MRQBelow', 'reutShortTermDebt_MRYAbove', 'reutShortTermDebt_MRYBelow', 'reutCurrentLiabilities_MRQAbove', 'reutCurrentLiabilities_MRQBelow', 'reutCurrentLiabilities_MRYAbove', 'reutCurrentLiabilities_MRYBelow', 'reutCashFromOperating_MRQAbove', 'reutCashFromOperating_MRQBelow', 'reutCashFromOperating_MRYAbove', 'reutCashFromOperating_MRYBelow', 'reutRevenuePerShare_MRYAbove', 'reutRevenuePerShare_MRYBelow', 'reutReturnOnAvgAssetsPct_MRYAbove', 'reutReturnOnAvgAssetsPct_MRYBelow', 'reutReturnOnInvestmentPct_MRYAbove', 'reutReturnOnInvestmentPct_MRYBelow', 'reutCapitalExpenditures_MRQAbove', 'reutCapitalExpenditures_MRQBelow', 'reutCapitalExpenditures_MRYAbove', 'reutCapitalExpenditures_MRYBelow', 'reutCashFromFinancing_MRQAbove', 'reutCashFromFinancing_MRQBelow', 'reutCashFromFinancing_MRYAbove', 'reutCashFromFinancing_MRYBelow', 'reutCashFromInvesting_MRQAbove', 'reutCashFromInvesting_MRQBelow', 'reutCashFromInvesting_MRYAbove', 'reutCashFromInvesting_MRYBelow', 'reutInterestExpense_MRQAbove', 'reutInterestExpense_MRQBelow', 'reutInterestExpense_MRYAbove', 'reutInterestExpense_MRYBelow', 'reutOperatingIncome_MRQAbove', 'reutOperatingIncome_MRQBelow', 'reutOperatingIncome_MRYAbove', 'reutOperatingIncome_MRYBelow', 'reutEnterpriseValueToEBITDAAbove', 'reutEnterpriseValueToEBITDABelow', 'ibPrice1WKPctAbove', 'ibPrice1WKPctBelow', 'ibPrice4WKPctAbove', 'ibPrice4WKPctBelow', 'ibPrice13WKPctAbove', 'ibPrice13WKPctBelow', 'ibPrice52WKPctAbove', 'ibPrice52WKPctBelow', 'reutYieldAbove', 'reutYieldBelow', 'reutCash_MRQAbove', 'reutCash_MRQBelow', 'reutEBITDA_MRQAbove', 'reutEBITDA_MRQBelow', 'reutPrice2SalesRatio_MRQAbove', 'reutPrice2SalesRatio_MRQBelow', 'reutPrice2CashFlowPerShare_MRQAbove', 'reutPrice2CashFlowPerShare_MRQBelow', 'reutTotalAssets_MRQAbove', 'reutTotalAssets_MRQBelow', 'reutTotalLiabilities_MRQAbove', 'reutTotalLiabilities_MRQBelow', 'reutTotalCommonEquity_MRQAbove', 'reutTotalCommonEquity_MRQBelow', 'reutTotalLongTermDebt_MRQAbove', 'reutTotalLongTermDebt_MRQBelow', 'reutBookValueTanPerShare_MRQAbove', 'reutBookValueTanPerShare_MRQBelow', 'reutCashFlowPerShare_TTMAbove', 'reutCashFlowPerShare_TTMBelow', 'reutEPS_TTMAbove', 'reutEPS_TTMBelow', 'reutRev2Share_TTMAbove', 'reutRev2Share_TTMBelow', 'sharesOutstandingAbove', 'sharesOutstandingBelow', 'minPeRatio', 'maxPeRatio', 'reutReturnOnInvestmentPct_TTMAbove', 'reutReturnOnInvestmentPct_TTMBelow', 'reutPretaxMarginPct_MRYAbove', 'reutPretaxMarginPct_MRYBelow', 'reutEnterpriseValueCurAbove', 'reutEnterpriseValueCurBelow', 'btMarketCapCriteria', 'marketCapAbove1e6', 'marketCapBelow1e6', 'marketCapAbove1e6', 'marketCapBelow1e6', 'researchProviderIs', 'market_edgeAbove', 'market_edgeBelow', 'market_graderAbove', 'market_graderBelow', 'morningstarAbove', 'morningstarBelow', 'constructs_researchAbove', 'constructs_researchBelow', 'sadif_analyticsAbove', 'sadif_analyticsBelow', 'street_ratingsAbove', 'street_ratingsBelow', 'validea_guruAbove', 'validea_guruBelow', 'valuengineAbove', 'valuengineBelow', 'zacksAbove', 'zacksBelow', 'market_edgeAbove', 'market_edgeBelow', 'market_graderAbove', 'market_graderBelow', 'morningstarAbove', 'morningstarBelow', 'constructs_researchAbove', 'constructs_researchBelow', 'sadif_analyticsAbove', 'sadif_analyticsBelow', 'street_ratingsAbove', 'street_ratingsBelow', 'validea_guruAbove', 'validea_guruBelow', 'valuengineAbove', 'valuengineBelow', 'zacksAbove', 'zacksBelow']\n"
     ]
    }
   ],
   "source": [
    "# parse XML document\n",
    "import xml.etree.ElementTree as ET\n",
    "tree = ET.fromstring(xml)\n",
    "\n",
    "# find all tags that are available for filtering\n",
    "tags = [elem.text for elem in tree.findall('.//AbstractField/code')]\n",
    "print(len(tags), 'tags:')\n",
    "print(tags)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice how ``abovePrice`` is now called ``priceAbove``...\n",
    "\n",
    "Using three of these filter tags, let's perform a query to find all US stocks that went up 20% and have a current price between 5 and 50 dollar, sorted by percentage gain:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n"
     ]
    }
   ],
   "source": [
    "sub = ScannerSubscription(\n",
    "    instrument='STK',\n",
    "    locationCode='STK.US.MAJOR',\n",
    "    scanCode='TOP_PERC_GAIN')\n",
    "\n",
    "tagValues = [\n",
    "    TagValue(\"changePercAbove\", \"20\"),\n",
    "    TagValue('priceAbove', 5),\n",
    "    TagValue('priceBelow', 50)]\n",
    "\n",
    "# the tagValues are given as 3rd argument; the 2nd argument must always be an empty list\n",
    "# (IB has not documented the 2nd argument and it's not clear what it does)\n",
    "scanData = ib.reqScannerData(sub, [], tagValues)\n",
    "\n",
    "symbols = [sd.contractDetails.contract.symbol for sd in scanData]\n",
    "print(symbols)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Any scanner query that TWS can do can alse be done through the API. The `scanCode` parameter maps directly to the \"Parameter\" window in the TWS \"Advanced Market Scanner.\" We can verify this by printing out the `scanCode` values available:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "762 scan codes, showing the ones starting with \"TOP\":\n",
      "['TOP_PERC_GAIN', 'TOP_PERC_LOSE', 'TOP_TRADE_COUNT', 'TOP_TRADE_RATE', 'TOP_PRICE_RANGE', 'TOP_VOLUME_RATE', 'TOP_OPEN_PERC_GAIN', 'TOP_OPEN_PERC_LOSE', 'TOP_AFTER_HOURS_PERC_GAIN', 'TOP_AFTER_HOURS_PERC_LOSE', 'TOP_OPT_IMP_VOLAT_GAIN', 'TOP_OPT_IMP_VOLAT_LOSE', 'TOP_STOCK_BUY_IMBALANCE_ADV_RATIO', 'TOP_STOCK_SELL_IMBALANCE_ADV_RATIO', 'TOP_STOCK_BUY_REG_IMBALANCE_ADV_RATIO', 'TOP_STOCK_SELL_REG_IMBALANCE_ADV_RATIO', 'TOP_PERC_GAIN', 'TOP_PERC_LOSE', 'TOP_TRADE_RATE', 'TOP_PERC_GAIN']\n"
     ]
    }
   ],
   "source": [
    "scanCodes = [e.text for e in tree.findall('.//scanCode')]\n",
    "\n",
    "print(len(scanCodes), 'scan codes, showing the ones starting with \"TOP\":')\n",
    "print([sc for sc in scanCodes if sc.startswith('TOP')])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Queries are not limited to stocks. To get a list of all supported instruments:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'BOND.AGNCY', 'ETF.FI.US', 'STOCK.EU', 'SSF.NA', 'BOND.MUNI', 'BOND.GOVT', 'BOND.CD', 'SSF.HK', 'WAR.EU', 'STOCK.HK', 'SSF.EU', 'IND.EU', 'IND.HK', 'FUND.ALL', 'STK', 'FUT.HK', 'BOND', 'FUT.US', 'STOCK.NA', 'BOND.GOVT.NON-US', 'PORTSTK', 'Global', 'IND.US', 'FUT.EU', 'STOCK.ME', 'SLB.US', 'NATCOMB', 'FUT.NA', 'ETF.EQ.US'}\n"
     ]
    }
   ],
   "source": [
    "instrumentTypes = set(e.text for e in tree.findall('.//Instrument/type'))\n",
    "print(instrumentTypes)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To find all location codes:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['STK.US', 'STK.US.MAJOR', 'STK.NYSE', 'STK.AMEX', 'STK.ARCA', 'STK.NASDAQ', 'STK.NASDAQ.NMS', 'STK.NASDAQ.SCM', 'STK.BATS', 'STK.US.MINOR', 'STK.PINK', 'STK.OTCBB', 'ETF.EQ.US', 'ETF.EQ.US.MAJOR', 'ETF.EQ.ARCA', 'ETF.EQ.NASDAQ.NMS', 'ETF.EQ.BATS', 'ETF.FI.US', 'ETF.FI.US.MAJOR', 'ETF.FI.ARCA', 'ETF.FI.NASDAQ.NMS', 'ETF.FI.BATS', 'FUT.US', 'FUT.GLOBEX', 'FUT.ECBOT', 'FUT.IPE', 'FUT.NYBOT', 'FUT.NYMEX', 'FUT.NYSELIFFE', 'FUT.CFE', 'FUT.CFECRYPTO', 'FUT.CMECRYPTO', 'FUT.ICECRYPTO', 'IND.US', 'BOND.WW', 'BOND.US', 'BOND.EU.EURONEXT', 'BOND.CD.US', 'BOND.AGNCY.US', 'BOND.GOVT.US', 'BOND.MUNI.US', 'BOND.GOVT.NON-US', 'BOND.GOVT.US.NON-US', 'BOND.GOVT.EU.EURONEXT', 'BOND.GOVT.HK.SEHK', 'SLB.PREBORROW', 'FUND.ALL', 'FUND.US', 'FUND.NON-US', 'STK.NA', 'STK.NA.CANADA', 'STK.NA.TSE', 'STK.NA.VENTURE', 'STK.NA.MEXI', 'FUT.NA', 'FUT.NA.CDE', 'FUT.NA.MEXDER', 'SSF.NA', 'SSF.NA.MEXDER', 'STK.EU', 'STK.EU.VSE', 'STK.EU.SBF', 'STK.EU.IBIS', 'STK.EU.IBIS-XETRA', 'STK.EU.IBIS-EUSTARS', 'STK.EU.IBIS-USSTARS', 'STK.EU.IBIS-ETF', 'STK.EU.IBIS-NEWX', 'STK.EU.BVME', 'STK.EU.AEB', 'STK.EU.BM', 'STK.EU.SFB', 'STK.EU.SWISS', 'STK.EU.VIRTX', 'STK.EU.EBS', 'STK.EU.LSE', 'STK.EU.OTHER', 'STK.EU.PRA', 'STK.EU.CPH', 'STK.EU.HEX', 'STK.EU.OSE', 'STK.EU.BVL', 'STK.EU.MOEX', 'FUT.EU', 'FUT.EU.BELFOX', 'FUT.EU.DTB', 'FUT.EU.FTA', 'FUT.EU.IDEM', 'FUT.EU.UK', 'FUT.EU.ICEEU', 'FUT.EU.LMEOTC', 'FUT.EU.MEFFRV', 'FUT.EU.MONEP', 'FUT.EU.OMS', 'FUT.EU.SOFFEX', 'IND.EU', 'IND.EU.BELFOX', 'IND.EU.DTB', 'IND.EU.FTA', 'IND.EU.ICEEU', 'IND.EU.MONEP', 'SSF.EU', 'SSF.EU.DTB', 'SSF.EU.IDEM', 'SSF.EU.ICEEU', 'SSF.EU.MEFFRV', 'SSF.EU.OMS', 'SSF.EU.SOFFEX', 'WAR.EU.ALL', 'STK.ME', 'STK.ME.TASE', 'STK.HK', 'STK.HK.TSE_JPN', 'STK.HK.SEHK', 'STK.HK.SEHKNTL', 'STK.HK.SEHKSZSE', 'STK.HK.ASX', 'STK.HK.NSE', 'STK.HK.SGX', 'FUT.HK', 'FUT.HK.HKFE', 'FUT.HK.KSE', 'FUT.HK.NSE', 'FUT.HK.OSE_JPN', 'FUT.HK.SGX', 'FUT.HK.SNFE', 'IND.HK', 'IND.HK.HKFE', 'IND.HK.KSE', 'IND.HK.NSE', 'IND.HK.OSE_JPN', 'IND.HK.SGX', 'IND.HK.SNFE', 'SSF.HK', 'SSF.HK.HKFE', 'SSF.HK.KSE', 'SSF.HK.NSE', 'SSF.HK.SGX', 'NATCOMB', 'NATCOMB.OPT.US', 'NATCOMB.OPT.AMEX', 'NATCOMB.OPT.CBOE', 'NATCOMB.OPT.ISE', 'NATCOMB.OPT.PHLX', 'NATCOMB.OPT.PSE', 'NATCOMB.GLOBEX', 'BACKTEST']\n"
     ]
    }
   ],
   "source": [
    "locationCodes = [e.text for e in tree.findall('.//locationCode')]\n",
    "print(locationCodes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "ib.disconnect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
